<template>
    <div class="func-center-page">
        <div class="search-box">
            <div class="input-container">
                <i class="search-icon" v-html="inputSearchSvg"></i>
                <input type="text" class="native-input" ref="msearchInputRef" v-model="mSearchInput"
                    v-debounce="[filterByFunctionName, `input`, 500]" placeholder="搜索" @input="onInput" />
                <i v-if="mSearchInput" class="clear-icon" @click="clearSearchContent">×</i>
            </div>
        </div>

        <div class="navigation-area" id="footer-funcNav"
            v-show="functionTree && functionTree.ChildGroup && functionTree.ChildGroup.length > 0">
            <template v-for="(funcGroup) in functionTree.ChildGroup" :key="funcGroup.ID">
                <div class="navigation-padding">
                    <div class="navigation-funcGroup">
                        {{ funcGroup.Name }}
                    </div>
                    <div class="navigation-funcList">
                        <template v-for="func in funcGroup.Functions" :key="func.ID">
                            <div class="navigation-funcItem"
                                @click="funcOpen({ fid: func.ID, appCode: func.AppID }, $event)" :id="func.ID">
                                <div class="navigation-funcItem-icon">
                                    <i :class="func.Icon ? func.Icon : DEFAULT_FUNCTION_ICON"></i>
                                </div>
                                <span class="navigation-funcItem-text" :title="func.Name"
                                    v-html="highlightKeyword(func.Name, mSearchInput)"></span>
                            </div>
                        </template>
                    </div>
                </div>
            </template>
        </div>

        <div class="noResult"
            v-show="functionTree && functionTree.ChildGroup && functionTree.ChildGroup.length == 0 && mSearchInput">
            <img src="/img/noSearchResult.png" class="img-searchNoImg">
            <span class="img-searchNoTxt">{{ searchNoResultFunc }}</span>
        </div>
    </div>
</template>

<script setup>
import { inputSearchSvg, DEFAULT_FUNCTION_ICON } from '@/constants/svg'
import { ref, onMounted, computed } from 'vue'
import { getApplicationInfo } from '@/service/funcMenuService'
import { highlightKeyword } from '@/service/commonService'


const mSearchInput = ref('')
const searchNoResultFunc = computed(() => {
    if (!mSearchInput.value) {
        return '暂无相关功能'
    }
    return `${'未找到和'}【${mSearchInput.value}】${'相关功能'}`;
});
const filterByFunctionName = (val) => {

}
const functionTree = ref([])
const clearSearchContent = () => {
    mSearchInput.value = ''
}
const onInput = (event) => {
    // 可以在这里添加额外的输入处理逻辑
    // 由于已经使用了v-model和v-debounce，这里可以为空
}
getApplicationInfo().then(res => {
    functionTree.value = res
})

</script>

<style scoped lang="scss">

.func-center-page {
    display: flex;
    flex-direction: column;
    height: 100%;
    height: 100%;
    flex: 1;
}

.navigation-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow-y: auto;
    /* 确保内容可滚动 */
    width: 100%;
    /* 确保宽度占满 */
}

.navigation-single {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.navigation-single-area {
    flex: 1;
    overflow-y: auto;
}

.navigation-padding {
    padding-bottom: 8px;
    margin-top: 10px;
    background: #fff;

    .navigation-funcGroup {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        padding: 16px 0 16px 16px;
        color: rgba(0, 0, 0, 0.9);
        font-weight: bold;
        line-height: 22px;
        font-size: 14px;
        max-width: calc(100vw - 16px);

    }

    .navigation-funcList {
        padding: 0 8px;
        display: flex;
        flex-wrap: wrap;

        .navigation-funcItem {
            flex-basis: 20%;
            margin-bottom: 16px;
            max-width: 100px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            display: flex;
            align-items: center;
            flex-direction: column;
            cursor: pointer;

            .navigation-funcItem-icon {
                width: 28px;
                height: 28px;
                margin-bottom: 10px;
                margin-top: 4px;
                border-radius: 12px;
                display: flex;
                font-size: 28px;
                justify-content: center;
                align-items: center;
            }

            .navigation-funcItem-text {
                color: rgba(0, 0, 0, 0.56);
                font-size: 12px;
                overflow: hidden;
                max-width: 84px;
                text-align: center;
                white-space: break-spaces;
                padding: 0 6px;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
                text-overflow: ellipsis;
            }
        }
    }
}

.search-box {
    padding: 8px 16px;
    background-color: #fff;

    .input-container {
        position: relative;
        display: flex;
        align-items: center;
        background-color: #f5f5f5;
        border-radius: 8px;
        padding: 0 10px;
        height: 32px;

        .search-icon {
            display: flex;
            align-items: center;
            color: #999;
            margin-right: 6px;
            position: relative;
            top: 1px;
        }

        .native-input {
            flex: 1;
            border: none;
            background: transparent;
            height: 100%;
            outline: none;
            font-size: 14px;
            color: #333;

            &::placeholder {
                color: #999;
            }
        }

        .clear-icon {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background-color: #ccc;
            color: #fff;
            font-size: 14px;
            cursor: pointer;
            line-height: 1;
        }
    }
}

.noResult {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-bottom: 30px;

    .img-searchNoTxt {
        text-align: center;
        white-space: pre-wrap;
        word-wrap: break-word;
        word-break: break-all;
    }
}
</style>